package com.ifinc.common.socket.client;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-30
 * Time: 00:47
 */

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;

/**
 * 消息解码器
 *
 */
public class ClientMessageDecoder extends LengthFieldBasedFrameDecoder {

	public ClientMessageDecoder(int maxFrameLength, int lengthFieldOffset,
                                int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {

		super(maxFrameLength, lengthFieldOffset, lengthFieldLength,
				lengthAdjustment, initialBytesToStrip);
	}

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			ChannelBuffer buffer) throws Exception {

/*		ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer);
		if (frame == null) {
			return null;
		}
        byte[] bb = frame.array();
        System.out.println("new String(bb) = " + new String(bb));
//		byte[] bytes = new byte[frame.readInt()];
//		frame.readBytes(bytes);

//		String content = new String(bytes);
//        System.out.println("content = " + content);
//		return content;
        return new String(bb);*/
        return super.decode(ctx,channel,buffer);
	}

	@Override
	protected Object decodeLast(ChannelHandlerContext ctx, Channel channel,
			ChannelBuffer buffer) throws Exception {

		return this.decode(ctx, channel, buffer);
	}

}
